SELECT (Stock.Type) As [Type]
,(Stock.Srl) As [Serial]
,(Stock.DocDate) As [Doc.Date]
,(Master.Name) As [Party Name]
,(Stock.Code) As [Product]
,(ItemMaster.Name) As [Product Description]
,(CASE WHEN ItemMaster.GSTFlag<>'C' THEN  CAST(Stock.Qty*-1 As Decimal(12,0)) ELSE  0 END ) As [Quantity]
,(Stock.tariff) As [HSN]
,(Address.GSTNo) As [GSTIN]
,(Statemaster.GSTCode+ ' -  '+Statemaster.Name) As [Place Of Supply]
,(SELECT top 1 G.Name FROM ITEMMASTER P  INNER JOIN ITEMMASTER G ON P.GRP=G.CODE WHERE P.code=stock.code) As [Reverse Charges]
,(Sales.InvoiceType) As [Invoice Type]
,( CAST(Stock.Taxable As Decimal(14,2))) As [Taxable]
,(Stock.GSTTaxCode) As [GST RATE]
,(CASE WHEN Taxmastergst.GSTTaxtype='GST'  THEN TaxMasterGST.SGSTTaxPerc ELSE '' END ) As [SGST %]
,(CASE WHEN Taxmastergst.GSTTaxtype='GST'  THEN  CAST(Stock.SGSTAmt As Decimal(14,2)) ELSE  0 END ) As [SGST Amt]
,(CASE WHEN Taxmastergst.GSTTaxtype='GST'  THEN TaxMasterGST.CGSTTaxPerc ELSE '' END ) As [CGST %]
,(CASE WHEN Taxmastergst.GSTTaxtype='GST'  THEN  CAST(Stock.CGSTAmt As Decimal(14,2)) ELSE  0 END ) As [CGST Amt]
,(CASE WHEN Taxmastergst.GSTTaxtype='IGST' THEN TaxMasterGST.IGSTTaxPerc ELSE '' END ) As [IGST %]
,(CASE WHEN Taxmastergst.GSTTaxtype='IGST' THEN  CAST(Stock.IGSTAmt As Decimal(14,2)) ELSE  0 END ) As [IGST Amt]

,(Stock.CessGSTPerc) As [Cess %]
,( CAST(Stock.CessGSTAmt As Decimal(14,2))) As [Cess Amt]
,(CASE WHEN Stock.FirstSno<>0 THEN  CAST(Sales.Roundoff As Decimal(14,2)) ELSE  0 END ) As [Round Off]
,(CASE WHEN Stock.FirstSno<>0 THEN  CAST(Sales.Amt As Decimal(14,2)) ELSE  0 END ) As [Total Amt]
,(Sales.eInvAckNo) As [Ack no.]
,(Sales.OtherRefNo) As [Other Ref]
,(Sales.OtherRefDate) As [Other Ref Dt]
,(SELECT TOP 1 M.Name FROM Ledger L INNER JOIN Master M ON M.Code = L.AltCode WHERE L.Sno = '00001'  AND L.Type = Sales.Type   AND L.Prefix = Sales.Prefix  AND L.Srl = Sales.Srl) As [Ledger Name]

FROM  Master INNER JOIN Sales ON Master.Code = Sales.Code AND Master.Branch = Sales.Branch  
LEFT OUTER JOIN Address ON Address.Code = Sales.Code AND Address.Sno = Sales.AltAddress AND Address.Branch = Sales.Branch  
LEFT OUTER JOIN LocationMaster ON Sales.Branch = LocationMaster.Branch AND Sales.LocationCode = LocationMaster.Code 
INNER JOIN Stock  
LEFT OUTER JOIN ItemMaster ON Stock.Code = ItemMaster.Code 
ON Sales.Branch=Stock.Branch AND Sales.Type=Stock.Type AND Sales.Prefix=Stock.Prefix And Sales.Srl = Stock.Srl  
INNER JOIN TaxMasterGST ON Stock.GSTTaxCode=TaxMasterGST.Code AND Stock.Branch=TaxMasterGST.Branch  
Left outer join StateMaster ON Sales.PlaceOfSupplyStateCode=Statemaster.Code 
WHERE 
(Stock.DocDate between '01 Apr 2025 00:00:00' AND '31 Mar 2026 23:59:59')  
AND (Sales.Type IN ('CNN','ONL','CLI','VOU','CSC','CSQ','RBV','ACC','ORC','SWI','INV','RBQ','RBR','RDN','IDN','INS','CSR','FSC','RBC','CSD','SAS','FIR','PIR','PVS','CVS','WSS','SML','MVS','SAL','MIS','CLL','SOD','ON1','SOB','SIF','OR1','SOP','EXS','SOT','SRR','CSH','RCM','CUS','SRN','SRP','SRB','SRT','DRG','DRN','CRG','GCN','CRN')) 
AND (Master.Branch = 'HO0000')  AND (Stock.Branch = 'HO0000') AND (Sales.Branch = 'HO0000') 
AND Sales.LocationCode IN('000007','000001','000003','000005','000004','000009','000008','000010','000006','000002')  
Order By 2,3,4